﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HiddenFileManager
{
    class FileItemList : List<FileItem>
    {
        readonly public String ListFile;

        public FileItemList(String listFile)
        {
            ListFile = listFile;
            Load();
        }

        void Load()
        {
            this.Clear();
            if (File.Exists(ListFile))
            {
                String[] fullPaths = ProtectedFile.ReadAllLines(ListFile);
                AddItemsCore(fullPaths);
            }
        }

        public void AddItems(String[] fullPaths)
        {
            AddItemsCore(fullPaths);
            Save();
        }
        void AddItemsCore(String[] fullPaths)
        {
            foreach (String fullPath in fullPaths)
            {
                this.Add(new FileItem(fullPath));
            }

            // Do not add duplicate items
            IList<FileItem> distinct = this.Distinct().ToList();
            this.Clear();
            this.AddRange(distinct);
            this.Sort();
        }

        public void Save()
        {
            ProtectedFile.WriteAllLines(ListFile, GetFullPaths());
        }

        String[] GetFullPaths()
        {
            String[] names = new String[this.Count];
            for (int i = 0; i < this.Count; i++)
            {
                names[i] = this[i].FullPath;
            }
            return names;
        }


    }
}
